home *** CD-ROM | disk | FTP | other *** search
- /* Application skeleton by Dean Yu */
- /* Main routine and initialization routines */
-
- // If the interfaces aren't included yet, do the standard includes. Otherwise, precompiled headers
- // have been loaded already, thank you very much
- #ifndef __TYPES__
- #include <Types.h>
- #include <AppleEvents.h>
- #include <Desk.h>
- #include <Dialogs.h>
- #include <Editions.h>
- #include <Events.h>
- #include <Fonts.h>
- #include <Menus.h>
- #include <SegLoad.h>
- #include <TextUtils.h>
- #include <ToolUtils.h>
- #include <Windows.h>
- #endif
-
- // Not in Metrowerks default precompiled headers
- #include <GestaltEqu.h>
-
- #include "FloaterSample.h"
- #include "appleEventHandlers.h"
- #include "eventHandler.h"
- #include "menuDispatch.h"
- #include "trapAvail.h"
-
- /* Private routine prototypes */
-
- static void InitStuff(void);
- static void SetUpMenus(void);
- static void SetUpWindows(void);
- static void SetupRecordingFloater(void);
- static void SetupToolsFloater(void);
- static void SetupTestWindows(void);
- static void SetUpLimits(void);
- static void SetUpAppleEvents(void);
-
- /* Global variables */
-
- Boolean gDone; /* Set to true if user selects Quit */
- Boolean gInBackground; /* Set to true if app is switched into background */
- short gTimeToDialogDismissal;
- Rect gDragArea; /* Area in which a window can be dragged */
- Rect gGrowBounds; /* Min and maximum size a window can be sized to */
- Boolean gNotificationPosted = false;// True if a notification was posted
- NMRec gNotificationRec; // the notification record
- WindowRef gRecordingFloater; // Reference to recording floating window
- WindowRef gToolsFloater; // Reference to tools floating window
-
- // UPPs
- AEEventHandlerUPP gOAPPHandlerUPP, gODOCHandlerUPP, gPDOCHandlerUPP, gQUITHandlerUPP;
- ActivateHandlerUPP gRecordingFloaterActivateHandler; // The UPPs for my windows
- ActivateHandlerUPP gToolsFloaterActivateHandler;
- ActivateHandlerUPP gActivateEventHandler;
-
- ActivateHandlerUPP gTestActivateEventHandler;
-
-
- void main()
- {
- MaxApplZone();
-
- InitStuff();
- EventLoop();
- }
-
- void InitStuff()
- {
- EventRecord firstEvent;
- OSErr gestaltError;
- long gestaltResponse;
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- InitDialogs(nil);
- InitCursor();
-
- if (!TrapAvailable(_Gestalt)) /* Look for _Gestalt */
- FatalErrorAlert(kSystemVersionTooOld);
-
- WaitNextEvent(everyEvent, &firstEvent, 60, nil);
- FlushEvents(everyEvent, 0);
-
- /* Check to see if certain features are implemented */
-
- gestaltError = Gestalt(gestaltAppleEventsAttr, &gestaltResponse); /* Check for AppleEvents™ */
- if (!gestaltError)
- {
- if ((gestaltResponse & (1 << gestaltAppleEventsPresent)) == 0)
- FatalErrorAlert(kNoAppleEvents);
- }
- else
- FatalErrorAlert(kGestaltError);
-
- gestaltError = Gestalt(gestaltPPCToolboxAttr, &gestaltResponse); /* Check for PPC Toolbox */
- if (gestaltError)
- FatalErrorAlert(kGestaltError);
-
- SetUpMenus();
- SetUpWindows();
- SetUpLimits();
- SetUpAppleEvents();
- }
-
-
- /* Get the menu bar from resource fork. Fill menu with DAs. Draw it */
-
- void SetUpMenus()
- {
- Handle theMenu;
-
- theMenu = GetNewMBar(rMbarID);
- SetMenuBar(theMenu);
- DisposHandle(theMenu);
- AddResMenu(GetMHandle(rAppleMenuID),'DRVR');
- DisableItem(GetMHandle(rEditMenuID), 0);
- CheckItem(GetMHandle(rFloatersMenuID), kRecordingItem, true);
- CheckItem(GetMHandle(rFloatersMenuID), kToolsItem, true);
-
- // For testing
- if(Button())
- {
- MenuHandle mhndl;
-
- // Add the "Test" menu
- mhndl = GetMenu(132);
- if(mhndl != nil)
- {
- (*mhndl)->menuID = 132;
- InsertMenu(mhndl, 0);
- }
- }
-
- DrawMenuBar();
- }
-
- /*
- Set up any windows needed by the application
- */
-
- void SetUpWindows()
- {
- gActivateEventHandler = NewActivateHandlerProc((ProcPtr)ActivateEventHandler);
-
- SetupRecordingFloater();
- SetupToolsFloater();
-
- // hold down mouse button for test mode
- if(Button())
- SetupTestWindows();
- }
-
- void SetupRecordingFloater()
- {
- Rect theRect;
- PicHandle recordingControlsPicture;
- OSErr error;
-
- recordingControlsPicture = (PicHandle) GetPicture(128);
- theRect = (**recordingControlsPicture).picFrame;
- OffsetRect(&theRect, 70, 70);
-
- gRecordingFloaterActivateHandler = NewActivateHandlerProc((ProcPtr)RecordingFloaterActivateHandler);
- error = NewWindowReference(&gRecordingFloater, &theRect, "\pRecord", true,
- kHasPaletteTitlebarMask, (WindowRef) -1, 0, gRecordingFloaterActivateHandler);
- SetWindowPic( gRecordingFloater, recordingControlsPicture);
- }
-
- void SetupToolsFloater()
- {
- Rect theRect;
- PicHandle toolsPicture;
- OSErr error;
-
- toolsPicture = (PicHandle) GetPicture(130);
- theRect = (**toolsPicture).picFrame;
- OffsetRect(&theRect, 170, 100);
-
- gToolsFloaterActivateHandler = NewActivateHandlerProc((ProcPtr)ToolsFloaterActivateHandler);
- error = NewWindowReference(&gToolsFloater, &theRect, "\pTools", true,
- kHasPaletteTitlebarMask, (WindowRef) -1, 0, gToolsFloaterActivateHandler);
- SetWindowPic( gToolsFloater, toolsPicture);
- }
-
- void SetupTestWindows()
- {
- Rect theRect;
- OSErr error;
- WindowAttributes attributes;
- WindowRef wind;
- PicHandle pict;
-
- // First a few floaters with various variation codes
- gTestActivateEventHandler = NewActivateHandlerProc((ProcPtr)TestFloaterActivateHandler);
-
- pict = GetPicture(132);
- if(pict == nil)
- return;
-
- theRect = (**pict).picFrame;
- ReleaseResource((Handle)pict);
- pict = nil;
-
- OffsetRect(&theRect, 200, 140);
-
- // Make a few floaters with different attributes
-
- // a close box
- attributes = kHasPaletteTitlebarMask + kHasCloseBoxMask;
- error = NewWindowReference(&wind, &theRect, "\pYawk", true,
- attributes, (WindowRef) -1, 0, gTestActivateEventHandler);
-
- // add a zoom box
- attributes += kHasZoomBoxMask;
- OffsetRect(&theRect, 20, 20);
- error = NewWindowReference(&wind, &theRect, "\pHibberly", true,
- attributes, (WindowRef) -1, 0, gTestActivateEventHandler);
-
- // add a grow box, no zoom box, side bar
- attributes = attributes + kHasGrowBoxMask + kHasSideTitleBarMask - kHasZoomBoxMask;
- OffsetRect(&theRect, 20, 20);
- error = NewWindowReference(&wind, &theRect, "\pFrenality", true,
- attributes, (WindowRef) -1, 0, gTestActivateEventHandler);
-
- // Now some odd doc windows
- attributes = kHasRoundedTitlebarMask + kHasCloseBoxMask;
- OffsetRect(&theRect, -140, -100);
- theRect.right += 50;
- error = NewWindowReference(&wind, &theRect, "\pPiffle", true,
- attributes, (WindowRef) -1, 0, gActivateEventHandler);
-
- attributes = kHasRoundedTitlebarMask; // no close box
- OffsetRect(&theRect, 20, 10);
- theRect.right += 50;
- error = NewWindowReference(&wind, &theRect, "\pWiffle", true,
- attributes, (WindowRef) -1, 0, gActivateEventHandler);
-
- // Try creating some windows from WIND resources
- error = GetNewWindowReference(&wind, 129, (WindowRef) -1, gActivateEventHandler);
- if(error == noErr)
- ShowReferencedWindow(wind);
-
- error = GetNewWindowReference(&wind, 130, (WindowRef) -1, gActivateEventHandler);
- if(error == noErr)
- ShowReferencedWindow(wind);
-
- // a floater
- error = GetNewWindowReference(&wind, 131, (WindowRef) -1, gTestActivateEventHandler);
- if(error == noErr)
- ShowReferencedWindow(wind);
- }
-
- /*
- Set up areas for the screen in which windows can be dragged and sized.
- */
-
- void SetUpLimits()
- {
- RgnHandle deskTop;
-
- deskTop = GetGrayRgn();
- gDragArea = (**deskTop).rgnBBox;
- SetRect(&gGrowBounds, 48, 48,
- ((gDragArea.right) - (gDragArea.left)),((gDragArea.bottom) - (gDragArea.top)));
- }
-
- /* Set up the AppleEvent dispatch table */
-
- void SetUpAppleEvents()
- {
- OSErr installAppleEventError;
-
- // Install the required Apple Event Handlers
- gOAPPHandlerUPP = NewAEEventHandlerProc(OAPPHandler);
- gODOCHandlerUPP = NewAEEventHandlerProc(ODOCHandler);
- gPDOCHandlerUPP = NewAEEventHandlerProc(PDOCHandler);
- gQUITHandlerUPP = NewAEEventHandlerProc(QUITHandler);
-
- installAppleEventError = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, gOAPPHandlerUPP, 0, false);
- installAppleEventError += AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, gODOCHandlerUPP, 0, false);
- installAppleEventError += AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, gPDOCHandlerUPP, 0, false);
- installAppleEventError += AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, gQUITHandlerUPP, 0, false);
-
- if (installAppleEventError)
- FatalErrorAlert(kAEInstallError);
- }
-